var offer_curr_page = 1;
var offers_per_page = 5;
var offers_num_pages = Math.ceil(offers.length / offers_per_page);
if(offers_num_pages == 0){
	offers_num_pages = 1;
}

var request_curr_page = 1;
var requests_per_page = 5;
var requests_num_pages = Math.ceil(requests.length / requests_per_page);
if(requests_num_pages == 0){
	requests_num_pages = 1;
}


function editPageHeight(){
	var tmp = (requests_num > offers_num) ? requests_num : offers_num;
	if (tmp < 1){
		tmp = 1;
	}
	$("#successful-requests").height(120 + (tmp * 170));
	$("#successful-offers").height(120 + (tmp * 170));
	$("#requests-container").height((tmp * 170));
	$("#offers-container").height((tmp * 170));
	$("#vertical-bar").height(120 + (tmp * 170));
}


function makeRequestsPaginator(){
	$("#requests-paginator").paginate({
		count: requests_num_pages,
		start: request_curr_page,
		display: 5,
		border: true,
		border_color: '#CD9F33',
		text_color: '#6F4632',
		background_color: '#FAF2E1',	
		border_hover_color: '#B18110',
		text_hover_color: '#6F4632',
		background_hover_color: '#DCC388', 
		images: false,
		mouse: 'press',
		onChange: function(page){
			requestsDisplayPage(page);
			positionRequestsPaginator();
			$(".resize-100").load(function(){
				resize($(this), 100);
			});
			editPageHeight();
			replaceIdByName();
			replaceIdByPic();
		}
	});
}


function requestsDisplayPage(page){			
	var html = '';
	requests_num = 0;
	request_curr_page = page;
	for (var i = 0; i < requests_per_page; i++){
		var counter = ((page - 1) * requests_per_page) + i;
		
		if (typeof(requests[counter]) == "undefined"){
			if(html == ''){
				html += '<br /><span class="medium-text">No successful requests found. <br /><br /><a href="browse/">Browse </a> for new items now!</span>';
			}
			break;
		}
		requests_num++;
		console.log(requests[counter]);
		html += '<div class="item-container">';
		html += '<div class="img-container"><a href="itemdescription/' + requests[counter].offer_id + '"><img class="resize-100" src="resources/offer_images_thumbnail/' + requests[counter].item_pic + '" title="'+requests[counter].item_title+'"/></a></div>';
		html += '<div class="cnt-container normal-text">';
		html += '<a href="itemdescription/' + requests[counter].offer_id + '"><span class="medium-text">' + requests[counter].item_title + '</span></a><br />';
		html += 'Previous Owner: <span class="get-fb-name">' + requests[counter].fb_id + '</span><br />';
		html += 'Description: <br />' + requests[counter].item_desc.substr(0, 90);
		if(requests[counter].item_desc.length > 90){
			html += '...';
		}
		html += '</div>';
		html += '<div style="clear: both;"></div>';
		html += '</div>';
		html += '</div>';
	}

	$("#requests-container").html(html);
}

function positionRequestsPaginator(){
	var width = $('#requests-container').width();
	var length = requests_num_pages > 5 ? 5 : requests_num_pages;
	var actual = 60 + 60 + 25 * requests_num_pages;
	$('#requests-paginator').css('margin-left', (width - actual) / 2);
}



function makeOffersPaginator(){
	$("#offers-paginator").paginate({
		count: offers_num_pages,
		start: offer_curr_page,
		display: 5,
		border: true,
		border_color: '#CD9F33',
		text_color: '#6F4632',
		background_color: '#FAF2E1',	
		border_hover_color: '#B18110',
		text_hover_color: '#6F4632',
		background_hover_color: '#DCC388', 
		images: false,
		mouse: 'press',
		onChange: function(page){
			offersDisplayPage(page);
			positionOffersPaginator();
			$(".resize-100").load(function(){
				resize($(this), 100);
			});
			editPageHeight();
			replaceIdByName();
			replaceIdByPic();
		}
	});
}


function offersDisplayPage(page){			
	var html = '';
	offer_curr_page = page;
	offers_num = 0;
	for (var i = 0; i < offers_per_page; i++){
		var counter = ((page - 1) * offers_per_page) + i;
		
		if (typeof(offers[counter]) == "undefined"){
			if(html == ''){
				html += '<br /><span class="medium-text">Do you have stuff hidden under your bed? <br /><br /><a href="offerform/0">Create </a> a new offer now!</span>';
			}
			break;
		}
		offers_num++;
		console.log(offers[counter]);
		html += '<div class="item-container">';
		html += '<div class="img-container"><a href="itemdescription/' + offers[counter].offer_id + '"><img class="resize-100" src="resources/offer_images_thumbnail/' + offers[counter].item_pic + '" title="'+offers[counter].item_title+'"/></a></div>';
		html += '<div class="cnt-container normal-text">';
		html += '<a href="itemdescription/' + offers[counter].offer_id + '"><span class="medium-text">' + offers[counter].item_title + '</span></a><br />';
		html += 'New Owner: <span class="get-fb-name">' + offers[counter].requester_fb_id + '</span><br />';
		html += 'Description: <br />' + offers[counter].item_desc.substr(0, 90);
		if(offers[counter].item_desc.length > 90){
			html += '...';
		}
		html += '</div>';
		html += '<div style="clear: both;"></div>';
		html += '</div>';
		html += '</div>';
	}

	$("#offers-container").html(html);
}

function positionOffersPaginator(){
	var width = $('#offers-container').width();
	var length = offers_num_pages > 5 ? 5 : offers_num_pages;
	var actual = 60 + 60 + 25 * length;
	$('#offers-paginator').css('margin-left', (width - actual) / 2);
}

$(function(){
	makeRequestsPaginator();
	makeOffersPaginator();
	requestsDisplayPage(request_curr_page);
	offersDisplayPage(offer_curr_page);
	positionRequestsPaginator();
	positionOffersPaginator();
	editPageHeight();
});